<extend name="common/layouts"/>


<block name="content">

    <div class="kt-portlet">
        <div class="kt-portlet__head">
            <div class="kt-portlet__head-label">
                <h3 class="kt-portlet__head-title">
                    上传作业
                </h3>
            </div>
        </div>
        <div class="kt-portlet__body">

            <form class="kt-form kt-form--label-right" onsubmit="return false" id="storeForm">
                <div class="kt-portlet__body">

                    <div class="form-group row">
                        <label class="col-2 col-form-label">选择课程</label>
                        <div class="col-6">
                            <select name="class_id" class="form-control" id="class_id">
                                <foreach name="list" item="vo">
                                    <option value="{$vo['id']}">{$vo['class']}</option>
                                </foreach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-2 col-form-label">作业</label>
                        <div class="col-10">
                            <input type="file" name="task" id="task"/>
                        </div>
                    </div>
                </div>
                <div class="kt-portlet__foot">
                    <div class="kt-form__actions">
                        <div class="row">
                            <div class="col-2">
                            </div>
                            <div class="col-10">
                                <button class="btn btn-success" id="submit">提交</button>
                                <button class="btn btn-secondary">取消</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <!--end::Form-->
    </div>


</block>

<block name="js">
    <script>
        var fm = new FormData();

        $('#submit').click(function () {

            fm.append('class_id', $('#class_id').val());
            fm.append('task', $('#task')[0].files[0]);

            $.ajax({
                url: '/home/task/store',
                data: fm,
                dataType: 'json',
                type: 'post',
                contentType: false,
                //取消帮我们格式化数据，是什么就是什么
                processData: false,
                success: function (result) {
                    if (result.code == 0) {
                        toastr.success("新增成功");
                        window.history.go(-1);
                    } else {
                        toastr.error(result.msg);
                        return;
                    }
                }
            })
        })

    </script>
</block>